package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.User;
import com.kaishengit.service.UserService;

public class LoginServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		
		
		UserService userService = new UserService();
		
		User user = userService.findByUsernamePassword(username, password);
		
		if(user!=null) {
			session.setAttribute("session_user", user);
			response.sendRedirect("personmanager.jspx");
			
		}else {
			request.setAttribute("state","10001");
			request.getRequestDispatcher("WEB-INF/views/login.jsp").forward(request, response);
		}
		
		


	}

	
	public void init() throws ServletException {
	
	}

}
